package com.base.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

/**
 * 哈希算法工具类
 * @author chengmingwei
 * @date 2015-03-01 
 *
 */
public final class SHA1Util {
	static final Logger LOGGER = Logger.getLogger("SHA1Util");
	
	private final static String DIG_SHA1 ="SHA1";
	
	private final static char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5',  
        '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};  

	
	
	/**
	 * 将二进制密文转换成十六进制字符串
	 * @param bytes	二进制密文字节数组
	 * @param isUpperCase 是否将十六进制字符串转成大写，[true:转大写,false:小写]
	 * @return	返回十六进制字符串
	 */
	public final static String toHex(byte[] bytes,boolean isUpperCase){
	   int len = bytes.length;  
        StringBuilder buf = new StringBuilder(len * 2);  
        // 把密文转换成十六进制的字符串形式  
        for (int j = 0; j < len; j++) {  
            buf.append(HEX_CHARS[(bytes[j] >> 4) & 0x0f]);  
            buf.append(HEX_CHARS[bytes[j] & 0x0f]);  
        }
        return (isUpperCase) ? buf.toString().toUpperCase() : buf.toString();
//		int len = bytes.length;
//		StringBuilder sbStrs = new StringBuilder(len*2);
//		for(int i=0; i<len; i++){
////			byte byteData = bytes[i];
////			char _c = HEX_CHARS[(byteData >> 4 & 0x0f)];
////			char _c2 = HEX_CHARS[(byteData & 0x0f)];
////			sbStrs.append(_c).append(_c2);
//			sbStrs.append(HEX_CHARS[(bytes[i] >> 4) & 0x0f]);
//			sbStrs.append(HEX_CHARS[bytes[i] & 0x0f]);  
//			
//		}
//		String hexStr = sbStrs.toString();
//		hexStr = isUpperCase ? hexStr.toUpperCase() : hexStr;
//		LOGGER.info("hexStr="+hexStr);
//		return hexStr;
	}
	
	/**
	 * 将二进制密文转换成十六进制字符串
	 * @param bytes	二进制密文字节数组
	 * @return	返回十六进制字符串
	 */
	public final static String toHex(byte[] bytes){
		return toHex(bytes,false);
	}
	
	/**
	 * 将指定的字符串用SHA1 算法加密，并以十六进制字符串形式返回
	 * @param content	要加密的字符串
	 * @param isUpperCase 是否将十六进制字符串转成大写，[true:转大写,false:小写]
	 * @return	返回加密后的十六进字符串
	 */
	public final static String encode(String content,boolean isUpperCase){
		if (!StringHandler.isValidStr(content)) return null;  
		try {
			MessageDigest mdigest = MessageDigest.getInstance(DIG_SHA1);
			mdigest.update(content.getBytes());
			byte[] datas = mdigest.digest();
			return toHex(datas,isUpperCase);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw new RuntimeException(e);  
		}
	}
	
	/**
	 * 将指定的字符串用SHA1 算法加密，并以十六进制字符串形式返回
	 * @param content	要加密的字符串
	 * @return	返回加密后的十六进字符串
	 */
	public final static String encode(String content){
		try {
			MessageDigest mdigest = MessageDigest.getInstance(DIG_SHA1);
			byte[] datas = mdigest.digest(content.getBytes());
			return toHex(datas);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw new RuntimeException(e);  
		}
	}
	
	public static void main(String[] args){
		String str = "zy_7244";//张燕
		System.out.println(str);
		String out = encode(str,true);
		System.out.println(out);
		System.out.println("====================");
		
		String str2 = "jlx_8635";//江龙啸
		System.out.println(str2);
		String out2 = encode(str2,true);
		System.out.println(out2);
		System.out.println("====================");
		
		String str3 = "wqj_5653";//wuqiongjie
		System.out.println(str3);
		String out3 = encode(str3,true);
		System.out.println(out3);
		System.out.println("====================");
	}
}
